package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec702Entity;
import net.yunxinyong.cec.entity.Cec705OneEntity;
import net.yunxinyong.cec.entity.Cec705OneEntityExample;
import net.yunxinyong.cec.mapper.Cec705OneEntityMapper;
import net.yunxinyong.cec.service.Cec705OneService;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class Cec705OneServiceImpl implements Cec705OneService {
    @Autowired
    private Cec705OneEntityMapper cec705EntityMapper;

    @Override
    public PageBean findAll(int page, int rows, Cec705OneEntity cec705) {
        Cec705OneEntityExample cec705OneEntityExample = new Cec705OneEntityExample();
        Cec705OneEntityExample.Criteria criteria = cec705OneEntityExample.createCriteria();
        if (cec705.getId() != null) {
            criteria.andIdEqualTo(cec705.getId());
        }
        if (page != 0 && rows != 0) {
            PageHelper.startPage(page, rows);
        }
        List<Cec705OneEntity> list = cec705EntityMapper.selectByExample(cec705OneEntityExample);
        //设置分页信息
        PageBean<Cec705OneEntity> pageBean = new PageBean<>();
        PageInfo<Cec705OneEntity> pageInfo = new PageInfo<>(list);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageDatas(list);
        return pageBean;
    }

    @Override
    public List<Cec705OneEntity> findOneByBaseId(int baseid) {
        Cec705OneEntityExample cec705OneEntityExample = new Cec705OneEntityExample();
        Cec705OneEntityExample.Criteria criteria = cec705OneEntityExample.createCriteria();
        criteria.andBaseIdEqualTo(baseid);
        List<Cec705OneEntity> list = cec705EntityMapper.selectByExample(cec705OneEntityExample);
        return list;
    }

    @Override
    public int updateById(int id, int status) {
        Cec705OneEntity cec705OneEntity = new Cec705OneEntity();
        cec705OneEntity.setId(id);
        cec705OneEntity.setStatus(status);
        cec705OneEntity.setUpdateTime(new Date());
        int count = cec705EntityMapper.updateByPrimaryKeySelective(cec705OneEntity);
        return count;
    }

    @Override
    public int deleteById(int id) {
        Cec705OneEntity cec705OneEntity = new Cec705OneEntity();
        cec705OneEntity.setId(id);
        cec705OneEntity.setStatus(1);
        int count = cec705EntityMapper.updateByPrimaryKeySelective(cec705OneEntity);
        return count;
    }

    @Override
    public int insertOne(int baseid) {
        Cec705OneEntity cec705OneEntity = new Cec705OneEntity();
        cec705OneEntity.setBaseId(baseid);
        //更改表时间
        cec705OneEntity.setUpdateTime(new Date());
        //创建表时间
        cec705OneEntity.setCreateTime(new Date());
        int count = cec705EntityMapper.insertSelective(cec705OneEntity);
        return count;
    }
}
